package com.zjweu.hkg.Controller;
import com.zjweu.hkg.DTO.NodeRequest;
import com.zjweu.hkg.POJO.HydroNodeRelation;
import com.zjweu.hkg.POJO.Result;
import com.zjweu.hkg.POJO.River;
import com.zjweu.hkg.Servies.HydroNodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping("/hydro")
public class HydroController {
    @Autowired
    HydroNodeService hydroNodeService;
    /**
     * 根据节点名添加新节点
     * @param name
     * @return
     */
    @RequestMapping("/addNodeByName")
    public int addNodeByName(@RequestParam("nodeName") String name){
        return hydroNodeService.addNodeByName(name);
    }

    /**
     * 添加节点和关系
     * @param nodeRequest
     * @return
     */
    @PostMapping("/addNodesAndRelation")
    public ResponseEntity<Integer> addNodesAndRelation(@RequestBody NodeRequest nodeRequest){
        //默认值
        int result = hydroNodeService.addNodesAndRelation(nodeRequest);

        return ResponseEntity.ok(result);
    }

    /**
     * 根据List添加节点和关系
     * @param nodeRequestList
     * @return
     */
    @PostMapping("/addNodesAndRelationByList")
    public int addNodesAndRelationByList(List<NodeRequest> nodeRequestList){
        return hydroNodeService.addNodesAndRelationByList(nodeRequestList);
    }

    /**
     * 清除neo4j中的所有数据
     * @return
     */
    @PostMapping("/clearAllNodes")
    public ResponseEntity<Integer> clearAllNodes(){
        System.out.println("清空所有数据");
        hydroNodeService.clearAllNodes();
        return ResponseEntity.ok(0);
    }

    /**
     * 从neo4j中获取所有关系
     * @return
     */
    @RequestMapping("/getAllRelationFromNeo4j")
    public HydroNodeRelation[] getAll(){
        HydroNodeRelation[] hydroNodeRelations=hydroNodeService.getAll();
        System.out.println(Arrays.toString(hydroNodeRelations));
        return hydroNodeRelations;
    }

    /**
     * 从mysql中获取所有水文数据
     * @return
     */
    @RequestMapping("/getAllRiverFromMysql")
    public List<River> getAllRiverFromMysql(){
        return hydroNodeService.getAllRiverFromMysql();
    }

    /**
     * 从水文数据中建立关系
     * @throws IOException
     */
    @RequestMapping("/createRelationFromRiver")
    public void createRelationFromRiver() throws IOException {
        hydroNodeService.createRelationFromRiver();
    }

    /**
     * 获取编辑之后的数据并同步给mysql和neo4j
     * @param riverList
     * @return
     * @throws IOException
     */
    @RequestMapping("/applyChangesToMysqlAndNeo4j")
    public Result<String> applyChangesToMysqlAndNeo4j(@RequestBody List<River> riverList) throws IOException {
        System.out.println("水文"+riverList);
        hydroNodeService.applyChangesToMysqlAndNeo4j(riverList);
        return Result.success();
    }

}
